<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Enterprises extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	function error(){
		$this->load->view('error');
	}

public function CategoryCMS(){
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="CategoryCMS";
		$displayData['pTabSelection']=$pTabSelection;
		$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/CategoryCMS', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function addCategory(){
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="viewCategory";
		$displayData['pTabSelection']=$pTabSelection;
		$this->enterprisesmodel->addCategory($_POST);
		$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
		$categoryArray=$this->enterprisesmodel->getAllCategories();
		$displayData['categoryArray']=$categoryArray;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/viewCategory', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function editCategory($category_id){
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="viewCategory";
		$displayData['pTabSelection']=$pTabSelection;
		$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
		$categoryDetailsArray=$this->enterprisesmodel->getCategoryDetails($category_id);
		$displayData['categoryDetailsArray']=$categoryDetailsArray;
		$displayData['category_id']=$category_id;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/updateCategory', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function updateCategory(){
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="viewCategory";
		$displayData['pTabSelection']=$pTabSelection;
		$this->enterprisesmodel->updateCategory($_POST);
		$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
		$categoryArray=$this->enterprisesmodel->getAllCategories();
		$displayData['categoryArray']=$categoryArray;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/viewCategory', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function getCategory(){
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="viewCategory";
		$displayData['pTabSelection']=$pTabSelection;
		$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
		$categoryArray=$this->enterprisesmodel->getAllCategories();
		$displayData['categoryArray']=$categoryArray;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/viewCategory', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function addStore(){
		error_log("add store **** controller ".print_r($_POST, true));
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="";
		$_POST['files']=$_FILES;
		$displayData['pTabSelection']=$pTabSelection;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$store_id=$this->enterprisesmodel->addStore($_POST);
			$_POST['store_id']=$store_id;
			$this->enterprisesmodel->addTags($_POST);
			$storesArray=$this->enterprisesmodel->getAllStores();
			$displayData['storeArray']=$storesArray;
			$displayData['storeDetailsArray']=$storesArray;
			$displayData['sessionUserData']=$sessionUserData;
			$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
			$this->load->view('enterprises/viewStore', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}
	
	

	public function editStore($store_id){
		error_log("inside editStore....");
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$_POST['files']=$_FILES;
		$pTabSelection="viewStore";
		$displayData['pTabSelection']=$pTabSelection;
		$displayData['store_id']=$store_id;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$storeDetailsArray=$this->enterprisesmodel->getStoreDetails($store_id);
			$displayData['storeCategoriesArray']=$this->enterprisesmodel->getStoreCategories($store_id);
			$displayData['storeDetailsArray']=$storeDetailsArray;
			$storeArray=$this->enterprisesmodel->getAllstores();
			$displayData['storeArray']=$storeArray;
			$displayData['sessionUserData']=$sessionUserData;
			$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
			$displayData['allCategoriesArray']=$this->enterprisesmodel->getAllCategories();
			$this->load->view('enterprises/updateStore', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function updateStore(){
		error_log("inside updateStore.......");
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$_POST['files']=$_FILES;
		$pTabSelection="viewStore";
		$displayData['pTabSelection']=$pTabSelection;
		$this->enterprisesmodel->updateStore($_POST);
		$displayData['storeArray']=$this->enterprisesmodel->getAlphaStores($alpha='0-9');
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/viewStore', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function getStore($alpha){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$_POST['files']=$_FILES;
		$pTabSelection="viewStore";
		$displayData['pTabSelection']=$pTabSelection;
		$displayData['storeArray']=$this->enterprisesmodel->getAlphaStores($alpha);
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
			$this->load->view('enterprises/viewStore', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function StoreCMS(){
		//error_log("inside StoreCMS.......");
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="StoreCMS";
		$displayData['pTabSelection']=$pTabSelection;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
			$displayData['allCategoriesArray']=$this->enterprisesmodel->getAllCategories();
			$this->load->view('enterprises/StoreCMS', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}
	//-----------LOGIN & LOGOUT----------------------------------------------

	public function login(){
		$this->load->model('enterprisesmodel');
		$pTabSelection="";
		$displayData['pTabSelection']=$pTabSelection;
		$this->load->view('enterprises/login', $displayData);
	}

	public function logOut(){
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="";
		$displayData['pTabSelection']=$pTabSelection;
		$array_items = array('username' => '', 'logged_in' => '');
		$this->session->unset_userdata($array_items);
		$this->load->view('enterprises/logOut', $displayData);
	}

	public function authenticate(){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$username=$this->enterprisesmodel->authenticate($_POST);
		error_log("auth.............................".print_r($_POST, true));
		//var_dump($username);
		if (isset($username[0]['user_username']) && $_POST['username']==$username[0]['user_username']){
			$newdata = array(
					'user_id'  => $username[0]['user_id'],
					'username'  => $username[0]['user_username'],
					'user_permissions'  => $username[0]['user_permissions'],
					'logged_in' => TRUE
			);
			$this->session->set_userdata($newdata);
			$sessionUserData = $this->session->all_userdata();
		}else {
			$array_items = array('user_id' => '', 'username' => '', 'user_permissions' => '', 'logged_in' => '');
			$this->session->unset_userdata($array_items);
		}
		$pTabSelection="";
		$displayData['pTabSelection']=$pTabSelection;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/welcome', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}



	//------------------ADD,DELETE,EDIT USERS-------------------------------------------

	public function addUser(){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="addUser";
		$displayData['pTabSelection']=$pTabSelection;
		$displayData['allUsersArray']=$this->enterprisesmodel->getAllUsers();
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/addUser', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function insertUser(){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="addUser";
		$displayData['pTabSelection']=$pTabSelection;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$displayData['allUsersArray']=$this->enterprisesmodel->insertUser($_POST);
			$displayData['allUsersArray']=$this->enterprisesmodel->getAllUsers();
			$this->load->view('enterprises/addUser', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function deleteUser($userId){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="addUser";
		$displayData['pTabSelection']=$pTabSelection;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$this->enterprisesmodel->deleteUser($userId);
			$displayData['allUsersArray']=$this->enterprisesmodel->getAllUsers();
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/addUser', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function editUserPerm(){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="editUserPerm";
		$displayData['pTabSelection']=$pTabSelection;
		$displayData['allUsersArray']=$this->enterprisesmodel->getAllUsers();
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/editUserPerm', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function editUserPermission($userId){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="editUserPerm";
		$displayData['pTabSelection']=$pTabSelection;
		$displayData['userId']=$userId;
		$displayData['allUsersArray']=$this->enterprisesmodel->getAllUsers();
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/editUserPermission', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function updatePermissions(){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		//error_log("update permissions".print_r($_POST, true));
		$pTabSelection="editUserPerm";
		$displayData['pTabSelection']=$pTabSelection;
		$displayData['allUsersArray']=$this->enterprisesmodel->getAllUsers();
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->enterprisesmodel->updatePermissions($_POST);
			$this->load->view('enterprises/editUserPerm', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	//------------------ADD,DELETE,EDIT OFFERS---------------------------------------------


	public function CouponCMS()
	{
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="CouponCMS";
		$displayData['pTabSelection']=$pTabSelection;
		$sessionUserData = $this->session->all_userdata();
		$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
		$displayData['allCategoriesArray']=$this->enterprisesmodel->getAllCategories();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/CouponCMS', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}
	
	
	public function addCoupon(){
		error_log("add code **** controller ".print_r($_POST, true));
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="viewCoupon";
		$_POST['files']=$_FILES;
		$displayData['pTabSelection']=$pTabSelection;
		$sessionUserData = $this->session->all_userdata();
    	if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){	
    	$_POST['user_id'] = $sessionUserData['user_id'];
    	$sId = $_POST['code_store'][0];
    	$_POST['store_detail'] = $this->enterprisesmodel->getStoreDetailsById($sId);
    	$_POST['files'] = $_FILES;
    	//var_dump($_POST);die();
        $this->enterprisesmodel->addCoupon($_POST);
        $displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
        $storeId=$_POST['code_store']['0'];
        $displayData['storeId']=$storeId;
        $couponArray=$this->enterprisesmodel->getStoreCoupons($storeId);
	    $displayData['couponArray']=$couponArray;
		$displayData['sessionUserData']=$sessionUserData;
		$this->load->view('enterprises/viewCoupon', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function getCoupon($storeId=0){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="viewCoupon";
		$displayData['pTabSelection']=$pTabSelection;
		$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
		if(!isset($storeId) || $storeId==0){
			$storeId=$displayData['allStoresArray'][0]['store_id'];
		}
		$displayData['storeId']=$storeId;
		$couponArray=$this->enterprisesmodel->getStoreCoupons($storeId);
		$displayData['couponArray']=$couponArray;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/viewCoupon', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}
	
	public function editCoupon($coupon_id){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="viewHotel";
		$displayData['pTabSelection']=$pTabSelection;
		$CouponDetailsArray=$this->enterprisesmodel->getCouponDetails($coupon_id);
		$displayData['couponDetailsArray']=$CouponDetailsArray;
		$displayData['coupon_id']=$coupon_id;
		$displayData['pTabSelection']=$pTabSelection;
		$sessionUserData = $this->session->all_userdata();
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$couponArray=$this->enterprisesmodel->getAllCoupons();
			$occasionArray=$this->enterprisesmodel->getAllOccasions();
			$displayData['occasionArray']=$occasionArray;
			$displayData['couponStoresArray']=$this->enterprisesmodel->getCouponStores($coupon_id);
			$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
			$displayData['allBanksArray']=$this->enterprisesmodel->getAllBanks();
			$displayData['allCategoriesArray']=$this->enterprisesmodel->getAllCategories();
			$displayData['couponArray']=$couponArray;
			$this->load->view('enterprises/updateCoupon', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}

	public function updateCoupon(){
		$this->load->model('enterprisesmodel');
		$this->load->model('enterprisesmodel');
		$this->load->library('session');
		$pTabSelection="";
		$displayData['pTabSelection']=$pTabSelection;
		$sessionUserData = $this->session->all_userdata();
		$_POST['user_id'] = $sessionUserData['user_id'];
		$sId = $_POST['code_store'][0];
		$_POST['store_detail'] = $this->enterprisesmodel->getStoreDetailsById($sId);
		$_POST['files'] = $_FILES;
		$this->enterprisesmodel->updateCoupon($_POST);
		$displayData['allStoresArray']=$this->enterprisesmodel->getAllStores();
		$storeId=$_POST['code_store']['0'];
		$displayData['storeId']=$storeId;
		$couponArray=$this->enterprisesmodel->getStoreCoupons($storeId);
		$displayData['couponArray']=$couponArray;
		if(isset($sessionUserData['logged_in']) && $sessionUserData['logged_in']){
			$displayData['sessionUserData']=$sessionUserData;
			$this->load->view('enterprises/viewCoupon', $displayData);
		}else {
			$this->load->view('enterprises/logOut', $displayData);
		}
	}
		
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
